<?php


namespace App\Service\AdminHtml;


class AdminForm
{

    protected $module;
    protected $controller;

    // 表单页面
    public function __construct($module, $controller)
    {
        $this->module     = $module;
        $this->controller = $controller;
    }

    // 输入框
    public function formHidden($field, $val)
    {
        $html = ' value="'.$val.'"   name="'.$field.'"';

        return '<input type="hidden" class="input-text valid" '.$html.'/>';
    }

    // 输入框
    public function formShow($val)
    {
        return '<span>'.$val.'</span>';
    }

    // 输入框
    public function formInput($field, $val, $label = '', $width = '336px')
    {
        $html = ' value="'.$val.'"   name="'.$field.'" style="width:'.$width.';"';
        if($label){
            $html .= '  placeholder="请填写'.$label.'"  ';
        }

        return '<input type="text" class="input-text valid" '.$html.'/>';
    }

    // 日期时间
    public function formDateTime($field, $val, $label = '', $type = 'date')
    {

        if($type == 'datetime'){
            $width = '180px';
        } elseif($type == 'date'){
            $width = '120px';
        } elseif($type == 'time'){
            $width = '120px';
        }


        $html = ' value="'.$val.'"   name="'.$field.'"   id="'.$field.'" style="width:'.$width.';"';
        if($label){
            $html .= '  placeholder="请填写'.$label.'"  ';
        }

        // js
        $js = "<script type='text/javascript'>
        laydate.render({
            elem: '#{$field}'
            ,type: '{$type}'
          });
	    </script>";

        return '<input type="text" class="input-text" '.$html.'/>'.$js;
    }

    // 输入框：数字
    public function formNumber($field, $val, $width = '136px')
    {
        // <input type="number" min="1" onkeyup="this.value=this.value.replace(/\D/, '');" class="laypage_skip">
        $html = ' value="'.$val.'"   name="'.$field.'" style="width:'.$width.';"';

        return '<input type="number" min="0" class="input-text valid" onkeyup="this.value=this.value.replace(/\D/, \'\');" class="laypage_skip" '.$html.'/>';
    }

    // 输入框
    public function formTextarea($field, $val, $width = '336px')
    {

        return '<textarea name="'.$field.'" id="'.$field.'" class="textarea">'.$val.'</textarea>';
    }

    // 输入框
    public function formUeditor($field, $val)
    {
        $url    = url('/innerend/deal/ueditor');
        $script = " var	ue = UE.getEditor('".$field."',{serverUrl:\"".$url."\",});";  // ,{serverUrl:\"".$url."\",}

        return '<script name="'.$field.'" id="'.$field.'" type="text/plain">'.$val.'</script><script>'.$script.'</script>';
    }

    // 密码框
    public function formPassword($field, $val, $label = '', $width = '336px')
    {
        $html = ' value="'.$val.'"   name="'.$field.'" style="width:'.$width.';"';
        if($label){
            $html .= '  placeholder="请输入'.$label.'"  ';
        }

        return '<input type="password" class="input-text valid" '.$html.'/>';
    }


    // 单选框
    public function formRadio($field, $val, $data)
    {
        // <input type="radio" name="sType"  value="记者" checked="checked" id="sType1" /><label for="sType1">媒体</label>
        $all  = '';
        $html = ' type="radio"  name="'.$field.'" ';
        foreach($data as $key => $show){
            $check = '';
            if($key == $val){
                $check = ' checked="checked" ';
            }

            $_id    = $field.'_'.$key;
            $_label = '<label for="'.$_id.'">'.$show.'</label>';
            $radio  = '<input '.$html.$check.' value="'.$key.'" id="'.$_id.'" />';
            $all    .= $radio.$_label.'&nbsp;&nbsp;';
        }

        return $all;
    }

    // 单选框
    public function formCheckbox($field, $vals, $data)
    {
        // <input type="checkbox" value="财经" name="roleDetail[]"/>财经&nbsp;
        $all  = '';
        $html = ' type="checkbox"  name="'.$field.'[]" ';
        foreach($data as $key => $show){
            $check = '';
            if(in_array($key, $vals)){
                $check = ' checked="checked" ';
            }

            $_id    = $field.'_'.$key;
            $_label = '<label for="'.$_id.'">'.$show.'</label>';
            $radio  = '<input '.$html.$check.' value="'.$key.'" id="'.$_id.'" '.$_label;
            $all    .= $radio.'&nbsp;&nbsp;';
        }

        return $all;
    }

    // 下拉框（一维数组）
    public function formSelect($field, $val, $data)
    {
        $all  = '<option value="" >请选择...</option>';
        $html = ' <select onchange="$(\'#sComeFrom\').val($(this).val())" class="select" name="'.$field.'" style="width:55px;"> ';
        $html = ' <select class="select" name="'.$field.'"  id="'.$field.'" style="width:155px;"> ';
        foreach($data as $key => $show){
            $selected = '';
            if($key == $val){
                $selected = ' selected="selected" ';
            }
            $option = '<option value="'.$key.'" '.$selected.'>'.$show.'</option>';
            $all    .= $option;
        }

        return $html.$all.'</select>';
    }

    // 下拉框（二维数组）
    public function formSelectLists($field, $val, $lists, $val_field, $show_field)
    {
        $all  = '<option value="" >请选择...</option>';
        $html = ' <select onchange="$(\'#sComeFrom\').val($(this).val())" class="select" name="'.$field.'" style="width:55px;"> ';
        $html = ' <select class="select" name="'.$field.'"  id="'.$field.'" style="width:200px;"> ';
        foreach($lists as $key => $item){
            $selected = '';
            $_level   = '';
            if($val == $item[$val_field]){
                $selected = ' selected="selected" ';
            }
            if(!empty($item['level'])){
                $_level = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $item['level']).'|--';
            }
            $_show  = $_level.$item[$show_field];
            $_key   = $item[$val_field];
            $option = '<option value="'.$_key.'" '.$selected.'>'.$_show.'</option>';
            $all    .= $option;
        }

        return $html.$all.'</select>';
    }

    // ajax搜索
    public function ajaxSearch($field, $val, $ajaxFun)
    {
        $fun  = ucfirst($ajaxFun);
        $html = '<input type="text" class="input-text valid" value="'.$val.'" name="show'.$field.'" id="show'.$field.'"  style="width:145px;" onkeyup="searchUser(this,"'.$field.'");" autocomplete="off"/>';
        $html = '<input type="text" class="input-text valid" value=""  style="width:145px;" onkeyup="search'.$fun.'(this,\''.$field.'\');" autocomplete="off"/>';
        $html .= '<span id="show_'.$field.'_ID">'.$field.':'.$val.'</span>';
        $html .= '<input type="hidden" value="'.$val.'" name="'.$field.'" id="hide_'.$field.'_ID"/>';
        $html .= '<div style="display:none;position: absolute;top: 30px;z-index: 1000;border: 1px solid #c3c3c3;width:143px;background-color: #fff;" id="search_'.$field.'_List"></div>';

        return $html;
    }

    // ajax搜索（适应列表搜索的第三版）
    public function ajaxSearch2($field, $val, $ajaxAction, $valueFiled='id', $showFiled='username')
    {
        $html = '<input type="text" class="input-text valid" value="'.$val.'" name="show'.$field.'" id="show'.$field.'"  style="width:145px;" onkeyup="searchUser(this,"'.$field.'", "'.$showFiled.'");" autocomplete="off"/>';

        $url  = url('/'.$this->module.'/selector/'.$ajaxAction);
        $html = '<input type="text" class="input-text valid" value=""  style="width:145px;" onkeyup="searchList(\''.$url.'\', this,\''.$field.'\',\''.$valueFiled.'\',\''.$showFiled.'\');" autocomplete="off"/>';
        $html .= '<span id="show_'.$field.'_ID">'.$field.':'.$val.'</span>';
        $html .= '<input type="hidden" value="'.$val.'" name="'.$field.'" id="hide_'.$field.'_ID"/>';
        $html .= '<div style="display:none;position: absolute;top: 30px;z-index: 1000;border: 1px solid #c3c3c3;width:143px;background-color: #fff;" id="search_'.$field.'_List"></div>';

        return $html;
    }

    // ajax搜索
    public function ajaxSearchList($field, $vals, $ajaxFun, $show_id = 'id', $show_label = 'username')
    {
        $fun  = ucfirst($ajaxFun);
        $html = '<input type="text" class="input-text valid" value=""  style="width:145px;" onkeyup="search'.$fun.'(this, \''.$field.'\');" autocomplete="off"/>';
        $html .= '<span id="show_'.$field.'_list">';
        foreach($vals as $_val){
            $html .= '<span class="spanList" data-id="'.$_val[$show_id].'">'.$_val[$show_label].'&nbsp;<a href="javascript:;" onclick="$(this).parent().remove()"> 删</a></span>&nbsp;&nbsp;';
        }
        $html .= '</span>';

        //$html .= '<input type="hidden" value="'.$val.'" name="hide'.$fun.'ID" id="hide'.$fun.'ID"/>';
        $html .= '<input type="hidden" value="" name="'.$field.'" id="'.$field.'" />';

        $html .= '<div style="display:none;position: absolute;top: 30px;z-index: 1000;border: 1px solid #c3c3c3;width:143px;background-color: #fff;" id="search_'.$field.'_List"></div>';
        $html .= '<script>var id_'.$field.'_array=[];
            $("#search_'.$field.'_List").parent().find(".spanList").each(function(){
                //id_'.$field.'_array.push({id:$(this).attr(\'data-id\') ,name:$(this).attr(\'data-name\')});
                id_'.$field.'_array.push($(this).attr(\'data-id\'));
            })
            console.log(\''.$field.'\',id_'.$field.'_array)
            $("#'.$field.'").val(JSON.stringify(id_'.$field.'_array));</script>';

        return $html;
    }

    // ajax搜索(老版本)
    public function ajaxSearchList2($field, $vals, $ajaxFun, $show_id = 'id', $show_label = 'username')
    {
        $fun  = ucfirst($ajaxFun);
        $html = '<input type="text" class="input-text valid" value=""  style="width:145px;" onkeyup="searchUser(this, \''.$field.'List\');" autocomplete="off"/>';
        foreach($vals as $_val){
            $html .= '<span class="spanList" data-id="'.$_val[$show_id].'">'.$_val[$show_label].'&nbsp;<a href="javascript:;" onclick="$(this).parent().remove()"> 删</a></span>&nbsp;&nbsp;';
        }

        //$html .= '<input type="hidden" value="'.$val.'" name="hide'.$fun.'ID" id="hide'.$fun.'ID"/>';
        $html .= '<input type="hidden" value="" name="'.$field.'" id="'.$field.'" />';

        $html .= '<div style="display:none;position: absolute;top: 30px;z-index: 1000;border: 1px solid #c3c3c3;width:143px;background-color: #fff;" id="'.$field.'List"></div>';
        $html .= '<script>var id'.$field.'_array=[];
            $("#'.$field.'List").parent().find(".spanList").each(function(){
                //id'.$field.'_array.push({id:$(this).attr(\'data-id\') ,name:$(this).attr(\'data-name\')});
                id'.$field.'_array.push($(this).attr(\'data-id\'));
            })
            console.log(id'.$field.'_array)
            $("#'.$field.'").val(JSON.stringify(id'.$field.'_array));</script>';

        return $html;
    }

    // 构建图片
    public function formImage($field, $val)
    {
        $isDel = '';
        $show  = $val;
        if(empty($show)){
            $show  = "/static/upfile.png";
            $isDel = "style=\"display: none;\"";
        }
        $html = '<img id="'.$field.'" src="'.$show.'" style="z-index: 1;" height="128">';
        $html .= '<input type="hidden" style="width:300px" value="'.$val.'" id="'.$field.'_image" name="'.$field.'">';
        $html .= '<button class="btn btn-primary radius" type="button" id="delThumb" '.$isDel.'>删除图片</button>';
        $html .= '<script>get_file_fun("'.$field.'");$("#delThumb").on("click",function(){$("#'.$field.'").attr("src","/static/upfile.png");$("#'.$field.'_image").val("");$("#delThumb").hide();});</script>';

        return $html;
    }

    // 构建图片
    public function formShowImage($val)
    {
        $show = $val;
        if(empty($show)){
            $show = "/static/upfile.png";
        }
        $html = '<a href="'.$val.'" target="_blank"><img id="thumb" src="'.$show.'" style="z-index: 1;" height="128" /></a>';

        return $html;
    }


    // 构建以后
    public function makeItem($label, $formHtml, $msg = '')
    {
        $msgHtml   = $msg ? '&nbsp;<span style="color: darkgray;">（'.$msg.'）</span>' : '';
        $labelHtml = '<label class="form-label col-xs-3 col-sm-3">'.$label.'</label>';
        $formHtml  = '<div class="formControls col-xs-7 col-sm-7">'.$formHtml.$msgHtml.'</div>';
        $span      = '<span class="form-label col-xs-2 col-sm-2"></span>';
        $html      = '<div class="row cl">'.$labelHtml.$formHtml.$span.'</div>';


        return $html;
    }
}


